package cn.sky.demo.controller;

import cn.sky.demo.aspect.Log;
import cn.sky.demo.domain.vo.BankMoney;
import cn.sky.demo.enums.BusinessType;
import cn.sky.demo.service.AsyncService;
import cn.sky.demo.utils.LoanCalculatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author sky
 * @date 2025年11月11日 10:50
 * @description 实现贷款计算器
 * @since 1.0.0
 */
@RestController
@Validated
public class BankMController{

    @RequestMapping("/bankmoney")
    //@Log(title = "贷款计算器", businessType = BusinessType.OTHER)
    public BankMoney test(@Validated @RequestBody BankMoney bankMoney) {
        double loanMoney = bankMoney.getLoanMoney();
        int years = bankMoney.getYears();
        double yIns = bankMoney.getYIns();

        String type = bankMoney.getType();

        double totalMoney = 0.0;
        double totalInterests = 0.0;
        double[][] detail = new double[0][];

        switch (type) {
            case "1": //等额本息
                LoanCalculatorUtils.EqualPrincipalInterestResult result1 =
                        LoanCalculatorUtils.calculateEqualPrincipalInterest(loanMoney, years, yIns);
                totalMoney = result1.getTotalMoney();
                totalInterests = result1.getTotalInterests();
                detail = result1.getDetail();
                break;
            case "2": //等额本金
                LoanCalculatorUtils.EqualPrincipalResult result2 =
                        LoanCalculatorUtils.calculateEqualPrincipal(loanMoney, years, yIns);
                totalMoney = result2.getTotalMoney();
                totalInterests = result2.getTotalInterests();
                detail = result2.getDetail();
                break;
        }

        return BankMoney.builder()
                .loanMoney(loanMoney)
                .years(years)
                .yIns(yIns)
                .totalMoney(totalMoney)
                .totalInterests(totalInterests)
                .detail(detail)
                .build();
}}
